/*********************************************************
          File Name:copy_constructor_destructor.cpp
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Wed 16 Sep 2015 09:34:50 PM CST
**********************************************************/

#include <iostream>
#include <string>

// this class shall has a `copy constructor`
// which has been commented

class HasPtr
{
        public:
                HasPtr(const std::string &s = std::string()):
                        ps(new std::string(s)), i(0) {}
                //HasPtr(const HasPtr &hp);
                ~HasPtr()
                {
                        delete ps;
                        ps = nullptr;
                }
                HasPtr &operator=(const HasPtr &hp)
                {
                        // ignore memory leak
                        ps = new std::string(*hp.ps);

                        i = hp.i;

                        return *this;
                }

                HasPtr f(HasPtr hp)
                {
                        HasPtr res = hp;
                        return res;
                }
        private:
                std::string *ps;
                int i;
};
/*
HasPtr::HasPtr(const HasPtr &hp)
{
        ps = new std::string(*hp.ps);   // `hp.ps` is `string *`
        i = hp.i;
}
*/
int main()
{
        HasPtr s;

        HasPtr tmp;

        HasPtr crash;

        crash = s.f(tmp);
}
